using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Box2D.XNA;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage3 : Stage
    {
        protected List<SpotLight> _spotLightList = new List<SpotLight>();

        float GROUND_ALPHA = 0.2f;

        public Stage3(Game game) : base(game) { }

        public override void Restart()
        {
            foreach (VisibleObject b in actors)
            {
                b.Restart();
            }
            StageManager.ProtalCommand = _startProtal;
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.Restart();
        }

        public override void InitializeScenes()
        {
            base.InitializeScenes();

            
          
            Animation a = new Animation("images/stages/stage3/stage3");
            a.LayerDepth = 0.9f;
            _wallBackGround.Add(a);
            
            _wallBackGroungPos = new Vector2(-610, -285);

            Animation title = new Animation("images/title/stageTitle", new Rectangle(158, 226, 68, 40), new Point(68, 40));
            title.Scale = 1.2f;

            Transition tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(380, 185), 0, 1, 80, 220);//0, 366
            tra.Register(this);

            GroundObject grount;
            GroundObject underBund;
            SpotLight _spotLight;
            ElementPiece element;
            GameDoor door;
            GameKey key;
            Switch switch_1;
            ProtalGate protalSwitch;


            camera.MoveSpeed = 5f;

            _backGroundPos = new Vector2(-200,-600);

            grount = 
            StateFactory.CreateGround(this, 308, 443, 806, 16, 0);
            grount.Alpha = 0f; grount =
            StateFactory.CreateGround(this, 846, 397, 288, 22, 0.35f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 998, 349, 40, 24, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1182, 306, 252, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 956, 245, 70, 19, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 825, 192, 70, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 612, 135, 234, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1388, 370, 72, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1495, 305, 70, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 1834, 250, 474, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 2166, 306, 96, 19, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 2320, 374, 100, 19, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 2157, 436, 142, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 2672, 305, 468, 19, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 3119, 305, 282, 21, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 2489, 435, 146, 20, 0); grount.Alpha = GROUND_ALPHA; 
            //grount = new GroundObject(this._world, this); grount.Position = new Vector2(113, 422); grount.CurrentAnimation = new Animation("images/system/black", new Point(34, 28)); grount.Size = new Vector2(34, 28); grount.Rotation = 0f; Actors.Add(grount);
            protalSwitch = new ProtalGate("Stage3_Protal_1", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage2_1", "Stage2_1_Portal_2"); protalSwitch.Position = new Vector2(280, 385); Actors.Add(protalSwitch);
            door = new GameDoor("Spot_Door_S1", _world, this); door.Position = new Vector2(1834, 190); door.Size = new Vector2(60, 100); door.DoorID = "Spot_Door_1"; door.Alpha = 0.2f; Actors.Add(door);

            key = new GameKey("Spot_Key_S1", _world, this); key.Position = new Vector2(596, 108); key.Size = new Vector2(23, 35); key.KeyID = "Spot_Door_1"; key.Alpha = 0.3f;  Actors.Add(key);
            door = new GameDoor("Spot_Door_S2", _world, this); door.Position = new Vector2(2568, 244); door.Size = new Vector2(60, 100); door.DoorID = "Spot_Door_2"; door.Alpha = 0.2f; Actors.Add(door);

            key = new GameKey("Spot_Key_S2", _world, this); key.Position = new Vector2(2160, 409); key.Size = new Vector2(23, 35); key.KeyID = "Spot_Door_2"; key.Alpha = 0.3f; Actors.Add(key);
            
            protalSwitch = new ProtalGate("Stage3_Protal_2", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage4", "Stage4_Protal_1"); protalSwitch.Position = new Vector2(3146, 245); Actors.Add(protalSwitch);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(3378, 141); grount.CurrentAnimation = new Animation("images/system/black", new Point(240, 328)); grount.Size = new Vector2(240, 328); grount.Rotation = 0f; Actors.Add(grount);
            switch_1 = new Switch(_world, this);

            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(900, -98); _spotLight.Range = 548; _spotLight.ScanRange = 30; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);
            switch_1.AddTarget(_spotLight);

            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(594, -30); _spotLight.Range = 204; _spotLight.ScanRange = 24; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);
            _spotLight.IsLight = false; switch_1.AddTarget(_spotLight);
           
            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(1432, -170); _spotLight.Range = 562; _spotLight.ScanRange = 30; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);
            _spotLight.IsLight = false; switch_1.AddTarget(_spotLight);

            switch_1.Position = new Vector2(1182, 271); switch_1.Size = new Vector2(50, 50); Actors.Add(switch_1);
             switch_1 = new Switch(_world, this);
           
            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(2268, -59); _spotLight.Range = 522; _spotLight.ScanRange = 33; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);
            switch_1.AddTarget(_spotLight);
            
            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(2131, -59); _spotLight.Range = 498; _spotLight.ScanRange = 29; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);
            _spotLight.IsLight = false; switch_1.AddTarget(_spotLight);
            
            _spotLight = new SpotLight(this.Game, _world); _spotLight.Position = new Vector2(2957, -51); _spotLight.Range = 432; _spotLight.ScanRange = 33; _spotLight.BaseAngle = -90f; _spotLightList.Add(_spotLight);

            _spotLight.IsLight = false; switch_1.AddTarget(_spotLight);
            switch_1.Position = new Vector2(2491, 270); switch_1.Size = new Vector2(50, 50); Actors.Add(switch_1);

            underBund = StateFactory.CreatekillBund(this, 1445, 547, 1024, 64, 0); underBund.Alpha = 0.4f;
            underBund = StateFactory.CreatekillBund(this, 2469, 547, 1024, 64, 0); underBund.Alpha = 0.4f;
            underBund = StateFactory.CreatekillBund(this, 3141, 547, 320, 64, 0); underBund.Alpha = 0.4f;


             grount = new GroundObject(this._world, this); grount.Position = new Vector2(-299, 473); grount.CurrentAnimation = new Animation("images/system/black", new Point(16, 46)); grount.Alpha = 0f; grount.Size = new Vector2(16, 46); grount.Rotation = 0f; Actors.Add(grount);
             grount = new GroundObject(this._world, this); grount.Position = new Vector2(-452, 488); grount.CurrentAnimation = new Animation("images/system/black", new Point(290, 16)); grount.Alpha = 0f; grount.Size = new Vector2(290, 16); grount.Rotation = 0f; Actors.Add(grount);
             grount = new GroundObject(this._world, this); grount.Position = new Vector2(-235, 329); grount.CurrentAnimation = new Animation("images/system/black", new Point(308, 212)); grount.Alpha = 0f; grount.Size = new Vector2(308, 212); grount.Rotation = 0f; Actors.Add(grount);
             //protalSwitch = new ProtalGate("Stage3_Protal_3", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage5", "stageSnowProtal1"); protalSwitch.Position = new Vector2(-238, 173);  Actors.Add(protalSwitch);
             grount = new GroundObject(this._world, this); grount.Position = new Vector2(-447, 164); grount.CurrentAnimation = new Animation("images/system/black", new Point(118, 333));grount.Alpha=0f; grount.Size = new Vector2(118, 333); grount.Rotation = 0f; Actors.Add(grount);

            Font = ResourceManager.LoadSpriteFont("SpriteFont1");

            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f,false);

            _song = MusicManager.LoadSong("Music/cave");
            if (GameClass._musicString == "Music/cave")
            { }
            else
            {
                GameClass._musicString = "Music/cave";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

            _bloom = _gameClass._BloomComponent;
            _bloom.ScenrDraw += DrawBackGrounds;
            _bloom.ScenrDraw += DrawWalls;

            _startProtal = new ProtalCommand("stage3", "Stage3_Protal_1");
        }


        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            GraphicsDevice.Clear(Color.Black);
            _bloom.BeginDraw();
            GraphicsDevice.Clear(Color.Black);
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.OnDraw(spriteBatch, camera.Transform);
            _bloom.Draw();
            //base.OnDraw(spriteBatch, delta);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);
            spriteBatch.End();

            //DataNeedSave readOut = FileIo.Load();
            //string printout = readOut.PositionX.ToString();
            //printout += _spotLightList.First().InView.ToString();

            #region
            spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "X:"+player.testX.ToString(), new Vector2(0, 0), Color.Black);
            //spriteBatch.DrawString(Font, printout, new Vector2(20, 20), Color.Aqua);
            //spriteBatch.DrawString(Font, "X"+camera.Position.X+",Y"+camera.Position.Y,new Vector2(250,50),Color.Sienna,0,new Vector2(0,0),2,SpriteEffects.None,1);
            //spriteBatch.DrawString(Font, "X" + Game.GraphicsDevice.Viewport.Width +
            //    ",Y" + Game.GraphicsDevice.Viewport.Height, new Vector2(250, 50), Color.Sienna, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "CX" + camera.Position.X +
            //    ",CY" + camera.Position.Y, new Vector2(250, 150), Color.Aqua, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "FPS:" + fps, new Vector2(50, 0), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "PX" +(int) player.Position.X +
            //  ",PY" + (int)player.Position.Y, new Vector2(50, 40), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "CTX" + camera.Transform.Translation.X +
            //    ",CTY" + camera.Transform.Translation.Y, new Vector2(250, 200), Color.Aqua, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "normal:" + player.CollisionListener._normal, new Vector2(50, 300), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "right:" + player.CollisionListener.rightList.Count(), new Vector2(50, 200), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "left:" + player.CollisionListener.leftlList.Count(), new Vector2(50, 250), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "buttom:" + player.CollisionListener.buttonList.Count(), new Vector2(50, 150), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "vsX:" + _spotLight.vs.X + "vsY:" + _spotLight.vs.Y, new Vector2(50, 300), Color.Blue, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "w"+_spotLight._scene.Width, new Vector2(50, 250), Color.Red, 0, new Vector2(0, 0), 2f, SpriteEffects.None, 1);
            //spriteBatch.DrawString(Font, "sdf" + llk, new Vector2(50, 300), Color.Red, 0, new Vector2(0, 0), 2, SpriteEffects.None, 1);
            spriteBatch.End();
            #endregion
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            //camera.Update(delta);
            //a.Update(delta);
            foreach (SpotLight spotLight in _spotLightList)
                spotLight.Update(delta);
          //  MusicManager.FadeInMusic("first");
            //MusicManager.FadeOutMusic("first");
        }

        public override void Dispose()
        {
            base.Dispose();
            _bloom.ScenrDraw -= DrawBackGrounds;
            _bloom.ScenrDraw -= DrawWalls;
        }

        public override Stage Clone()
        {
            Stage3 stage = new Stage3(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
